/* 
 *   Date       Developer         Description
 *   2017-09-07 周成功      新建
 * 
 *
 *   Classname:  com.titan.demo.commons.DaoImpl.Player1MoneyDaoImpl
 * 
 *   The source code contained in this listing is proprietary to
 *   Baby Footmarks studio.
 * 
 *   Unauthorized copying, adaptation, distribution, use, or display
 *   is strictly prohibited.
 *   This software is Copyright 2017 Baby Footmarks studio.
 */
package com.titan.demo.commons.DaoImpl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Component;

import com.titan.core.base.BaseDao;
import com.titan.core.utils.db.JdbcUtils;
import com.titan.core.utils.object.Args;
import com.titan.demo.commons.RowMapper.Player1MoneyRowMapper;
import com.titan.demo.commons.domain.Player1MoneyDomain;
import com.titan.demo.commons.Where.Player1MoneyWhere;
import com.titan.demo.commons.dao.IPlayer1MoneyDao;


/**
 * 根据数据库表player1_money创建的DaoImpl类
 *
 * @author 周成功
 *
 */
@Component
public class Player1MoneyDaoImpl extends BaseDao implements IPlayer1MoneyDao {
    private static final long serialVersionUID = 1L;

    @Override
    public Player1MoneyDomain findByKey(String token, String id) throws Exception {
        assertNotNull("token", token);
        assertNotNull("id", id);
        
        String sql = "select * from player1_money  where id=?";
        Player1MoneyDomain domain = JdbcUtils.queryForObject(sql, new Player1MoneyRowMapper(), id);
        return domain;
    }
    
    @Override
    public Player1MoneyDomain findByKey(String token, Player1MoneyDomain domain) throws Exception {
        return findByKey(token, domain.getId());
    }
    
    public List<Player1MoneyDomain> findByWhere(String token, String[] fields, String where) throws Exception {
        assertNotNull("token", token);
        StringBuilder sql = buildSelect(fields);
        if( (where!=null)&&(!"".equals(where)) ) {
            sql.append(" where ").append(where);
        }
        return JdbcUtils.queryForList(sql.toString(), new Player1MoneyRowMapper());
    }
    
    public List<Player1MoneyDomain> findByWhere(String token, String[] fields, String where, int pageIndex, int pageSize) throws Exception {
        assertNotNull("token", token);
        StringBuilder sql = buildSelect(fields);
        if( (where!=null)&&(!"".equals(where)) ) {
            sql.append(" where ").append(where);
        }
        sql.append(" limit ").append((pageIndex-1)*pageSize).append(",").append(pageSize);
        return JdbcUtils.queryForList(sql.toString(), new Player1MoneyRowMapper());
    }

    
    public List<Player1MoneyDomain> findByWhere(String token, String[] fields, Player1MoneyWhere where) throws Exception {
        assertNotNull("token", token);
        StringBuilder sql = buildSelect(fields);
        if(where==null) {
            return JdbcUtils.queryForList(sql.toString(), new Player1MoneyRowMapper());
        } else {
            sql.append(where.toSql(true));
            return JdbcUtils.queryForList(sql.toString(), new Player1MoneyRowMapper(), where.toObjects());
        }
    }
    
    public List<Player1MoneyDomain> findByWhere(String token, String[] fields, Player1MoneyWhere where, int pageIndex, int pageSize) throws Exception {
        assertNotNull("token", token);
        StringBuilder sql = buildSelect(fields);
        if(where==null) {
            sql.append(" limit ").append((pageIndex-1)*pageSize).append(",").append(pageSize);
            return JdbcUtils.queryForList(sql.toString(), new Player1MoneyRowMapper());
        } else {
            sql.append(where.toSql(true));
            sql.append(" limit ").append((pageIndex-1)*pageSize).append(",").append(pageSize);
            return JdbcUtils.queryForList(sql.toString(), new Player1MoneyRowMapper(), where.toObjects());
        }
    }
    
    
    public List<Player1MoneyDomain> findByWhere(String token, String[] fields, String[][] where) throws Exception {
        assertNotNull("token", token);
        
        List<Object> args = new ArrayList<Object>();
        try {
            StringBuilder sql = buildSelect(fields);
            int len = where.length;
            for(int i=0; i<len; i++) {
                if(i==0) {
                    sql.append(" where ");
                } else {
                    sql.append(" and ");
                }
                sql.append(where[i][0]);
                args.add(where[i][1]);
            }
            return JdbcUtils.queryForList(sql.toString(), new Player1MoneyRowMapper(), args.toArray(new Object[args.size()]));
        } finally {
            args.clear();
            args = null;
        }
    }
    
    public List<Player1MoneyDomain> findByWhere(String token, String[] fields, String[][] where, int pageIndex, int pageSize) throws Exception {
        assertNotNull("token", token);
        
        List<Object> args = new ArrayList<Object>();
        try {
            StringBuilder sql = buildSelect(fields);
            int len = where.length;
            for(int i=0; i<len; i++) {
                if(i==0) {
                    sql.append(" where ");
                } else {
                    sql.append(" and ");
                }
                sql.append(where[i][0]);
                args.add(where[i][1]);
            }
            sql.append(" limit ").append((pageIndex-1)*pageSize).append(",").append(pageSize);
            return JdbcUtils.queryForList(sql.toString(), new Player1MoneyRowMapper(), args.toArray(new Object[args.size()]));
        } finally {
            args.clear();
            args = null;
        }
    }

    private static StringBuilder buildSelect(String[] fields) {
        StringBuilder sql = new StringBuilder(128);
        sql.append("select ");
        if( fields==null || fields.length==0 ) {
            sql.append("*");
        } else {
            int len = fields.length;
            for (int i = 0; i < len; i++) {
                if(i>0) {
                    sql.append(",");
                }
                sql.append(fields[i]);
            }
        }
        sql.append(" from player1_money");
        return sql;
    }
    private static StringBuilder buildSelect(List<Object> args, Player1MoneyDomain domain, String[] fields) {
        StringBuilder sql = buildSelect(fields);
        if(domain==null) {
            return sql;
        } else {
            buildWhere(sql, args, domain);
        }
        return sql;
    }
    private static void buildWhere(StringBuilder sql, List<Object> args, Player1MoneyDomain domain) {
        if(domain==null) {
            return;
        }
        int countWhere = 0;
        countWhere = countWhere + where("id", domain.getId(), sql, args, countWhere);
        countWhere = countWhere + where("createtime", domain.getCreatetime(), sql, args, countWhere);
        countWhere = countWhere + where("play", domain.getPlay(), sql, args, countWhere);
        countWhere = countWhere + where("playid", domain.getPlayid(), sql, args, countWhere);
        countWhere = countWhere + where("money_old", domain.getMoney_old(), sql, args, countWhere);
        countWhere = countWhere + where("money_var", domain.getMoney_var(), sql, args, countWhere);
        countWhere = countWhere + where("money_new", domain.getMoney_new(), sql, args, countWhere);
        countWhere = countWhere + where("create_name", domain.getCreate_name(), sql, args, countWhere);
        countWhere = countWhere + where("create_dt", domain.getCreate_dt(), sql, args, countWhere);
        countWhere = countWhere + where("update_name", domain.getUpdate_name(), sql, args, countWhere);
        countWhere = countWhere + where("update_dt", domain.getUpdate_dt(), sql, args, countWhere);
        countWhere = countWhere + where("delete_flag", domain.getDelete_flag(), sql, args, countWhere);
        countWhere = countWhere + where("delete_name", domain.getDelete_name(), sql, args, countWhere);
        countWhere = countWhere + where("delete_dt", domain.getDelete_dt(), sql, args, countWhere);
    }    
        

    public List<Player1MoneyDomain> findByWhere(String token, String[] fields, Player1MoneyDomain domain) throws Exception {
        assertNotNull("token", token);
        List<Object> args = new ArrayList<Object>();
        try {
            StringBuilder sql = buildSelect(args, domain, fields);        
            return JdbcUtils.queryForList(sql.toString(), new Player1MoneyRowMapper(), args.toArray(new Object[args.size()]));
        } finally {
            args.clear();
            args = null;
        }
    }

    @Override
    public List<Player1MoneyDomain> findByWhere(String token, String[] fields, Player1MoneyDomain domain, int pageIndex, int pageSize) throws Exception {
        assertNotNull("token", token);
        List<Object> args = new ArrayList<Object>();
        try {
            StringBuilder sql = buildSelect(args, domain, fields);
            sql.append(" limit ").append((pageIndex-1)*pageSize).append(",").append(pageSize);
            return JdbcUtils.queryForList(sql.toString(), new Player1MoneyRowMapper(), args.toArray(new Object[args.size()]));
        } finally {
            args.clear();
            args = null;
        }
    }
    
    
    public List<Player1MoneyDomain> findByWhere(String token, String where) throws Exception {
        return findByWhere(token, null, where);
    }
    public List<Player1MoneyDomain> findByWhere(String token, String where, int pageIndex, int pageSize) throws Exception {
        return findByWhere(token, null, where, pageIndex, pageSize);
    }
    public List<Player1MoneyDomain> findByWhere(String token, String[][] where) throws Exception {
        return findByWhere(token, null, where);
    }
    public List<Player1MoneyDomain> findByWhere(String token, String[][] where, int pageIndex, int pageSize) throws Exception {
        return findByWhere(token, null, where, pageIndex, pageSize);
    }
    public List<Player1MoneyDomain> findByWhere(String token, Player1MoneyWhere where) throws Exception {
        return findByWhere(token, null, where);
    }
    public List<Player1MoneyDomain> findByWhere(String token, Player1MoneyWhere where, int pageIndex, int pageSize) throws Exception {
        return findByWhere(token, null, where, pageIndex, pageSize);
    }
    public List<Player1MoneyDomain> findByWhere(String token, Player1MoneyDomain domain) throws Exception {
        return findByWhere(token, null, domain);
    }
    public List<Player1MoneyDomain> findByWhere(String token, Player1MoneyDomain domain, int pageIndex, int pageSize) throws Exception {
        return findByWhere(token, null, domain, pageIndex, pageSize);
    }
    
    
    public int findCountByWhere(String token, String where) throws Exception {
        assertNotNull("token", token);
        StringBuilder sql = new StringBuilder(128);
        sql.append("select count(1) from player1_money ");
        if( (where!=null)&&(!"".equals(where)) ) {
            sql.append(where);
        }
        return JdbcUtils.queryForInt(sql.toString());
    }
    public int findCountByWhere(String token, Player1MoneyWhere where) throws Exception {
        assertNotNull("token", token);
        StringBuilder sql = new StringBuilder(128);
        sql.append("select count(1) from player1_money ");
        if(where==null) {
            return JdbcUtils.queryForInt(sql.toString());
        } else {
            sql.append(where.toSql(false));
            return JdbcUtils.queryForInt(sql.toString(), where.toObjects());
        }
    }    
    public int findCountByWhere(String token, String[][] where) throws Exception {
        assertNotNull("token", token);
        List<Object> args = new ArrayList<Object>();
        StringBuilder sql = new StringBuilder(128);
        sql.append("select count(1) from player1_money ");
        int len = where.length;
        for(int i=0; i<len; i++) {
            if(i==0) {
                sql.append(" where ");
            } else {
                sql.append(" and ");
            }
            sql.append(where[i][0]);
            args.add(where[i][1]);
        }
        try {
            return JdbcUtils.queryForInt(sql.toString(), args.toArray(new Object[args.size()]));
        } finally {
            args.clear();
            args = null;
            sql = null;
        }
    }


    public int findCountByWhere(String token, Player1MoneyDomain domain) throws Exception {
        assertNotNull("token", token);
        List<Object> args = new ArrayList<Object>();
        StringBuilder sql = new StringBuilder(128);
        sql.append("select count(1) from player1_money");
        if(domain!=null) {
            int countWhere = 0;
            countWhere = countWhere + where("id", domain.getId(), sql, args, countWhere);
            countWhere = countWhere + where("createtime", domain.getCreatetime(), sql, args, countWhere);
            countWhere = countWhere + where("play", domain.getPlay(), sql, args, countWhere);
            countWhere = countWhere + where("playid", domain.getPlayid(), sql, args, countWhere);
            countWhere = countWhere + where("money_old", domain.getMoney_old(), sql, args, countWhere);
            countWhere = countWhere + where("money_var", domain.getMoney_var(), sql, args, countWhere);
            countWhere = countWhere + where("money_new", domain.getMoney_new(), sql, args, countWhere);
            countWhere = countWhere + where("create_name", domain.getCreate_name(), sql, args, countWhere);
            countWhere = countWhere + where("create_dt", domain.getCreate_dt(), sql, args, countWhere);
            countWhere = countWhere + where("update_name", domain.getUpdate_name(), sql, args, countWhere);
            countWhere = countWhere + where("update_dt", domain.getUpdate_dt(), sql, args, countWhere);
            countWhere = countWhere + where("delete_flag", domain.getDelete_flag(), sql, args, countWhere);
            countWhere = countWhere + where("delete_name", domain.getDelete_name(), sql, args, countWhere);
            countWhere = countWhere + where("delete_dt", domain.getDelete_dt(), sql, args, countWhere);
        }        
        try {
            return JdbcUtils.queryForInt(sql.toString(), args.toArray(new Object[args.size()]));
        } finally {
            args.clear();
            args = null;
            sql = null;
        }
    }
    

    public int insert(String token, Player1MoneyDomain domain) throws Exception {
        assertNotNull("token", token);
        assertNotNull("domain", domain);
        //String op = userinfo.getOpname();
        String op = "sys";
        
        String sql = "insert into player1_money(id,createtime,play,playid,money_old,money_var,money_new,create_name,create_dt,update_name,update_dt,delete_flag,delete_name,delete_dt) values (?,?,?,?,?,?,?,?,sysdate(),?,sysdate(),?,?,?)";
        return JdbcUtils.update(sql, domain.getId(), domain.getCreatetime(), domain.getPlay(), domain.getPlayid(), domain.getMoney_old(), domain.getMoney_var(), domain.getMoney_new(), op, op, domain.getDelete_flag(), domain.getDelete_name(), domain.getDelete_dt());
    }


    public int updateByKey(String token, String id, Args args) throws Exception {
        assertNotNull("token", token);
        assertNotNull("args", args);
        assertNotNull("id", id);
        //String op = userinfo.getOpname();
        String op = "sys";
        
        StringBuilder sql = new StringBuilder(256);
        sql.append("update player1_money");
        String[] keys = args.getKeys();
        List<Object> values = new ArrayList<Object>();
        for(int i=0; i<keys.length; i++) {
            if( i==0 ) {
                sql.append(" set ").append(keys[i]).append("=?");
            } else {
                sql.append(" , ").append(keys[i]).append("=?");
            }
            values.add(args.getValue(keys[i]));
        }
        sql.append(" , update_dt=sysdate()");
        sql.append(" , update_name=?");
        sql.append(" where id=?");
        values.add(op);
        values.add(id);
        
        return JdbcUtils.update(sql.toString(), values.toArray(new Object[values.size()]));
    }


    public int updateByKey(String token, String id, String fieldName, Object fieldValue) throws Exception {
        assertNotNull("token", token);
        assertNotNull("fieldName", fieldName);
        assertNotNull("fieldValue", fieldValue);
        assertNotNull("id", id);
        //String op = userinfo.getOpname();
        String op = "sys";
        
        String sql = "update player1_money set "+fieldName+"=?,update_dt=sysdate(),update_name=? where id=?";
        return JdbcUtils.update(sql, fieldValue, op, id);
    }

    public int updateByKey(String token, Player1MoneyDomain domain, String[] updateFields, String[] useSysdate) throws Exception {
        assertNotNull("token", token);
        assertNotNull("domain", domain);
        //String op = userinfo.getOpname();
        String op = "sys";
        
        Set<String> set = new HashSet<String>();
        set.add("create_dt");
        set.add("update_dt");
        set.add("delete_dt");
        if(useSysdate!=null) {
            for(int i=0; i<useSysdate.length; i++) {
                set.add(useSysdate[i].toLowerCase());
            }
        }        
        List<Object> args = new ArrayList<Object>();
        StringBuilder sql = new StringBuilder(128);
        sql.append("update player1_money set ");
        if(updateFields==null) {
            update(op, "id", domain.getId(), sql, args, set);
            update(op, "createtime", domain.getCreatetime(), sql.append(", "), args, set);
            update(op, "play", domain.getPlay(), sql.append(", "), args, set);
            update(op, "playid", domain.getPlayid(), sql.append(", "), args, set);
            update(op, "money_old", domain.getMoney_old(), sql.append(", "), args, set);
            update(op, "money_var", domain.getMoney_var(), sql.append(", "), args, set);
            update(op, "money_new", domain.getMoney_new(), sql.append(", "), args, set);
            update(op, "create_name", domain.getCreate_name(), sql.append(", "), args, set);
            update(op, "create_dt", domain.getCreate_dt(), sql.append(", "), args, set);
            update(op, "update_name", domain.getUpdate_name(), sql.append(", "), args, set);
            update(op, "update_dt", domain.getUpdate_dt(), sql.append(", "), args, set);
            update(op, "delete_flag", domain.getDelete_flag(), sql.append(", "), args, set);
            update(op, "delete_name", domain.getDelete_name(), sql.append(", "), args, set);
            update(op, "delete_dt", domain.getDelete_dt(), sql.append(", "), args, set);
        } else {
            int len = updateFields.length;
            for(int i=0; i<len; i++) {
                String field = updateFields[i].toLowerCase();
                if(i>0) {
                    sql.append(", ");
                }
                update(op, field, domain.getValue(field).getValue(), sql, args, set);
            }
        }
        args.add(domain.getValue("id").getValue());
        sql.append(" where id=?");
        try {
            return JdbcUtils.update(sql.toString(), args.toArray(new Object[args.size()]));
        } finally {
            args.clear();
            args = null;
            sql = null;
        }
    }


    @Override
    public int updateByKeySetAll(String token, Player1MoneyDomain domain) throws Exception {
        assertNotNull("token", token);
        assertNotNull("domain", domain);

        List<Object> args = new ArrayList<Object>();
        StringBuilder sql = new StringBuilder(128);
        sql.append("update player1_money set ");
        sql.append("id").append("=?"); args.add(domain.getId());
        sql.append(",createtime").append("=?"); args.add(domain.getCreatetime());
        sql.append(",play").append("=?"); args.add(domain.getPlay());
        sql.append(",playid").append("=?"); args.add(domain.getPlayid());
        sql.append(",money_old").append("=?"); args.add(domain.getMoney_old());
        sql.append(",money_var").append("=?"); args.add(domain.getMoney_var());
        sql.append(",money_new").append("=?"); args.add(domain.getMoney_new());
        sql.append(",create_name").append("=?"); args.add(domain.getCreate_name());
        sql.append(",create_dt").append("=?"); args.add(domain.getCreate_dt());
        sql.append(",update_name").append("=?"); args.add(domain.getUpdate_name());
        sql.append(",update_dt").append("=?"); args.add(domain.getUpdate_dt());
        sql.append(",delete_flag").append("=?"); args.add(domain.getDelete_flag());
        sql.append(",delete_name").append("=?"); args.add(domain.getDelete_name());
        sql.append(",delete_dt").append("=?"); args.add(domain.getDelete_dt());
        args.add(domain.getValue("id").getValue());
        sql.append(" where id=?");
        try {
            return JdbcUtils.update(sql.toString(), args.toArray(new Object[args.size()]));
        } finally {
            args.clear();
            args = null;
            sql = null;
        }
    }
    
    
    public int updateByKeySetNull(String token, String id, String...fields) throws Exception {
        assertNotNull("token", token);
        assertNotNull("fields", fields);
        assertNotNull("id", id);
        //String op = userinfo.getOpname();
        String op = "sys";
        
        StringBuilder sql = new StringBuilder(256);
        sql.append("update player1_money");
        for(int i=0; i<fields.length; i++) {
            if( i==0 ) {
                sql.append(" set ").append(fields[i]).append("=null");
            } else {
                sql.append(" , ").append(fields[i]).append("=null");
            }
        }
        sql.append(" , update_dt=sysdate()");
        sql.append(" , update_name=?");
        sql.append(" where id=?");
        
        return JdbcUtils.update(sql.toString(), op, id);
    }
    
    
    public int deleteByKey(String token, String id) throws Exception {
        assertNotNull("token", token);
        assertNotNull("id", id);
        String sql = "delete from player1_money  where id=?";
        return JdbcUtils.update(sql.toString(), id);
    }


    @Override
    public int deleteByKey(String token, Player1MoneyDomain domain) throws Exception {
        assertNotNull("token", token);
        assertNotNull("domain", domain);
        return deleteByKey(token, domain.getId());
    }
    
    
    @Override
    public int delete(String token, String where) throws Exception {
        assertNotNull("token", token);
        assertNotNull("where", where);
        StringBuilder sql = new StringBuilder("delete from player1_money");
        if( (where!=null)&&(!"".equals(where)) ) {
            sql.append(" where ").append(where);
        }        
        return JdbcUtils.update(sql.toString());
    }
    
    
    @Override
    public int delete(String token, String[][] where) throws Exception {
        assertNotNull("token", token);
        assertNotNull("where", where);
        List<Object> args = new ArrayList<Object>();
        StringBuilder sql = new StringBuilder("delete from player1_money");
        int len = where.length;
        for(int i=0; i<len; i++) {
            if(i==0) {
                sql.append(" where ");
            } else {
                sql.append(" and ");
            }
            sql.append(where[i][0]);
            args.add(where[i][1]);
        }
        try {
            return JdbcUtils.update(sql.toString(), args.toArray(new Object[args.size()]));
        } finally {
            args.clear();
            args = null;
            sql = null;
        }        
    }
    
    
    @Override
    public int delete(String token, Player1MoneyWhere where) throws Exception {
        assertNotNull("token", token);
        assertNotNull("where", where);
        StringBuilder sql = new StringBuilder("delete from player1_money");
        sql.append(where.toSql(false));
        return JdbcUtils.update(sql.toString(), where.toObjects());
    }
    
    
    @Override
    public int delete(String token, Player1MoneyDomain where) throws Exception {
        assertNotNull("token", token);
        assertNotNull("where", where);
        List<Object> args = new ArrayList<Object>();
        StringBuilder sql = new StringBuilder("delete from player1_money");
        buildWhere(sql, args, where);
        try {
            return JdbcUtils.update(sql.toString(), args.toArray(new Object[args.size()]));
        } finally {
            args.clear();
            args = null;
            sql = null;
        }        
    }
        

    public int closeByKey(String token, String id) throws Exception {
        assertNotNull("token", token);
        assertNotNull("id", id);
        //String op = userinfo.getOpname();
        String op = "sys";
        
        String sql = "update player1_money set delete_flag=1,delete_dt=sysdate(),delete_name=? where id=?";
        return JdbcUtils.update(sql, op, id);
    }


    public int openByKey(String token, String id) throws Exception {
        assertNotNull("token", token);
        assertNotNull("id", id);
        //String op = userinfo.getOpname();
        String op = "sys";
        
        String sql = "update player1_money set delete_flag=0,delete_dt=sysdate(),delete_name=? where id=?";
        return JdbcUtils.update(sql, op, id);
    }   
}

